package UI.LessonPlan
{
	import Events.UIInputBoxEvent;
	
	import Utility.*;
	
	import flash.display.*;
	import flash.events.KeyboardEvent;
	import flash.text.*;
	
	import mx.core.UIComponent;
	
	public class UIInputBox extends UIComponent
	{		
		private const MIN_TEXT_HEIGHT:Number = 10
		private const SIDE_MARGIN:Number=5;
			
		private var _title:TextField;
		private var _description:TextField;
		private var _response:TextField;
		
		public function UIInputBox(title:String,description:String="",response:String="")
		{
			super();
			this._title = generateTextField(title,true);
			this._description = generateTextField(description,false,true);
			this._response = generateTextField(response,false,false,true,uint(0xd9d9d9))
			this._response.addEventListener(KeyboardEvent.KEY_UP,expandResponseField);
				
			positionElements();
			addChildren();
		}
		
		public function clear():void
		{
			_response.text="";
			_response.height=Math.max(MIN_TEXT_HEIGHT,_response.textHeight)+5;
		}
		
		private function expandResponseField(e:KeyboardEvent):void
		{
			if(_response.height!=_response.textHeight+5)
			{
				_response.height=Math.max(MIN_TEXT_HEIGHT,_response.textHeight)+5;
				positionElements();
				dispatchEvent(new UIInputBoxEvent(UIInputBoxEvent.EXPAND));
			}
		}
		
		public function get title():String
		{
			return StringUtility.removeTrailingAndLeadingCharacter(" ",_title.text);
		}
		
		public function get description():String
		{
			return StringUtility.removeTrailingAndLeadingCharacter(" ",_description.text);
		}
		
		public function get response():String
		{
			return StringUtility.removeTrailingAndLeadingCharacter(" ",_response.text);
		}
		
		private function generateTextField(text:String,bold:Boolean=false,italic:Boolean=false,isInputField:Boolean=false,color:Object=null):TextField
		{
			var format:TextFormat = new TextFormat(null,null,null,bold,italic)
			format.align=TextFormatAlign.CENTER;
			var tf:TextField = new TextField();
			tf.text = text;
			tf.setTextFormat(format);
			tf.width = tf.textWidth+5;
			tf.height=Math.max(MIN_TEXT_HEIGHT,tf.textHeight)+5;
			tf.selectable = false;
			if(isInputField)
			{
				tf.type = TextFieldType.INPUT;
				tf.selectable = true;
				tf.multiline=true;
			}
			
			if(color!=null)
			{
				tf.background = true;
				tf.backgroundColor = uint(color);
			}
			return tf;
		}
		
		private function positionElements():void
		{
			stackElements(_title,_description,_response);
		}
		
		private function stackElements(... params):void
		{
			var yPos:Number=0;
			for each(var obj:DisplayObject in params)
			{
				obj.y=yPos;
				yPos=obj.y + obj.height;
			}
		}
		
		private function addChildren():void
		{
			addChild(_title);
			addChild(_description);
			addChild(_response);
		}
		
		public override function get width():Number
		{
			return Math.max(_title.width,_description.width,_response.width)+SIDE_MARGIN*2;
		}
		
		public override function set width(w:Number):void
		{
			_response.width = w-SIDE_MARGIN*2;
			_title.width=w-SIDE_MARGIN*2;
			_description.width = w-SIDE_MARGIN*2;
			positionElements();
		}
		
		public override function get height():Number
		{
			return _response.y+_response.height+5;
		}
	}
}